Skip to content

chore: Disallow reserve() in clippy to prevent panics#22386

Open
2010YOUY01 wants to merge 1 commit into
apache:mainfrom
2010YOUY01:clippy-no-reserve
Open

chore: Disallow reserve() in clippy to prevent panics#22386
2010YOUY01 wants to merge 1 commit into
apache:mainfrom
2010YOUY01:clippy-no-reserve

Conversation

@2010YOUY01
Copy link
Copy Markdown
Contributor

Which issue does this PR close?

  • Closes #.

Rationale for this change

In #22323, we have fixed panic bugs by replacing Vec::reserve() with Vec::try_reserve()

This PR enforces a clippy rule to disallow Vec::reserve() project-wise, to prevent future violations. Also it is easy to ignore this lint with macro, if we can ensure the safety.

What changes are included in this PR?

Are these changes tested?

They are not testable right now. To trigger the panic, we would need to construct an Arrow array with more than i32::MAX elements, but Arrow arrays are currently backed by flat vectors.

However, if Arrow supports an encoding such as RLE in the future, it may become possible to represent such large arrays with constant memory. At that point, these code paths would become vulnerable, so I think it is better to fix them now.

Are there any user-facing changes?

@github-actions github-actions Bot added functions Changes to functions implementation physical-plan Changes to the physical-plan crate spark labels May 20, 2026
Copy link
Copy Markdown
Contributor

@kosiew kosiew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@2010YOUY01
Thanks for the cleanup here. Everything looks good overall. I just had a couple of small suggestions around the clippy::disallowed_methods exemption rationale to help future readers understand the tradeoff more clearly.

block_size = block_size.saturating_mul(2);
}
let to_reserve = len.max(block_size as usize);
#[expect(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rationale on #[expect(clippy::disallowed_methods)] mentions that block sizing prevents capacity overflow, but Vec::reserve can still fail due to allocation failure. If this path is intentionally kept infallible because the surrounding API cannot return a Result, it would help to call that out explicitly in the reason. Otherwise, it may be worth tracking a follow-up for a fallible StringView builder path using try_reserve so allocation failures can be propagated cleanly.

if self.in_progress.capacity() < required_cap {
self.flush_in_progress();
let to_reserve = (length as usize).max(self.next_block_size() as usize);
#[expect(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thought here on the exemption rationale. The bounded block size helps avoid overflow issues, but it does not prevent allocation failure. It would be helpful to clarify that this is an intentional tradeoff for an infallible API path, or possibly reference a future follow-up for a fallible builder approach. That would make it clearer that this is a narrow exception rather than a general safe-use pattern for the lint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation physical-plan Changes to the physical-plan crate spark

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants